package com.cloudspace.jindun.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.activity.dianhua.LinkmanActivity;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class MyChannelDetailAdapter extends BaseAdapter  {
    private List<Person> mData;
    private FragmentActivity activity;

    private View.OnClickListener detailListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Person person = (Person) v.getTag();
            if (person != null && activity != null) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("person", person);

                Intent intent = new Intent(activity, LinkmanActivity.class);
                intent.putExtras(bundle);
                activity.startActivity(intent);
            }
        }
    };

    public MyChannelDetailAdapter(FragmentActivity activity){
        this.activity = activity;
    }

    public void setDatas(List<Person> data){
        if (ObjUtil.isEmpty(data)){
            return;
        }

        Collections.sort(data, new Comparator<Person>() {
            @Override
            public int compare(Person lhs, Person rhs) {
                if (lhs != null && rhs != null
                        && lhs.getName() != null && rhs.getName() != null){
                    return  lhs.getName().compareTo(rhs.getName());
                }

                return 0;
            }
        });
        this.mData = data;
        notifyDataSetChanged();
    }

    public List<Person> getData(){
        return mData;
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(mData)){
            return 0;
        }

        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(mData)){
            return null;
        }

        if (position < 0 || position >= mData.size()){
            return null;
        }

        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_channel_member, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.phone_image_header);
            holder.name = (TextView) convertView.findViewById(R.id.phone_textview_name);
            holder.detailImg = (ImageView) convertView.findViewById(R.id.phone_detail);
            holder.detailLy = convertView.findViewById(R.id.phone_detail_ly);
            holder.remarkTv = (TextView) convertView.findViewById(R.id.phone_textview_remark);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Person person = mData.get(position);

        holder.name.setText(mData.get(position).getName());
        holder.remarkTv.setText(mData.get(position).getRemarkName());
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.icon, person.getImageUrl(), Configs.IMAGE_CORNER_RADIUS);

        holder.detailLy.setOnClickListener(detailListener);
        holder.detailLy.setTag(person);
        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView name;
        TextView remarkTv;
        ImageView detailImg;
        View detailLy;
    }
}